package com.lam.monitor.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.filechooser.FileSystemView;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.lam.common.base.controller.BaseController;
import com.lam.common.base.domain.Result;
import com.lam.framework.operLog.annotation.Log;
import com.lam.monitor.domain.Server;

/**
 * 服务器监控
 * 
 * @author lam
 */
@Log(title = "服务监控")
@RestController
@RequestMapping("/${adminPath}/monitor/server")
public class ServerController extends BaseController {
	
	@Log(oper="查询服务器信息")
	@PreAuthorize("@ss.hasPm('monitor:server:info')")
	@GetMapping("info")
	public Result getInfo() throws Exception {
		Server server = new Server();
		server.copyTo();
		return Result.ok(server);
	}
	
	/**
  	 * @功能：获取磁盘信息
  	 * @return
  	 */
	@Log(oper="磁盘信息查询")
	@PreAuthorize("@ss.hasPm('monitor:server:diskInfo')")
  	@GetMapping("/queryDiskInfo")
  	public Result queryDiskInfo(){
  		
		// 当前文件系统类
        FileSystemView fsv = FileSystemView.getFileSystemView();
        // 列出所有windows 磁盘
        File[] fs = File.listRoots();
        logger.info("查询磁盘信息:"+fs.length+"个");
        List<Map<String,Object>> list = new ArrayList<>();
        
        for (int i = 0; i < fs.length; i++) {
        	if(fs[i].getTotalSpace()==0) {
        		continue;
        	}
        	Map<String,Object> map = new HashMap<>();
        	map.put("name", fsv.getSystemDisplayName(fs[i]));
        	map.put("max", fs[i].getTotalSpace()/1024/1024/1024);
        	long used = fs[i].getTotalSpace()-fs[i].getFreeSpace();
        	map.put("used", used/1024/1024/1024);
        	map.put("usedPercent", used*100/fs[i].getTotalSpace());
        	list.add(map);
        }
        return Result.ok(list);
  	}
}
